package utilities 
{
	import org.flixel.*;
	/**
	 * ...
	 * @author Andrew Lau
	 */
	public class AntiEmitter extends FlxEmitter 
	{
		public var radius:Number;
		
		/** 
		 * The speed factor of particles (.5 for half speed, 1 for normal speed, 2 for faster etc).
		 * The velocity of particles is equal to their radius.
		 * */
		private var _speed:Number;
		
		public function AntiEmitter(x:Number = 0, y:Number = 0, speed:Number = 1, radius:Number = 25, size:Number = 0) 
		{
			super(x, y, size);
			
			this.radius = radius;
			this.minParticleSpeed = new FlxPoint( -15, -15);
			this.maxParticleSpeed = new FlxPoint(15, 15);
			this._speed = 1;
		}
		
		override public function emitParticle():void
		{
			var particle:FlxParticle = recycle(FlxParticle) as FlxParticle;
			var pos:Array = randPos();
			particle.lifespan = lifespan;
			particle.elasticity = 0;
			particle.reset(this.x + pos[0] - (particle.width >> 1), this.y + pos[1] - (particle.height >> 1));
			particle.solid = false;
			particle.visible = true;
			
			particle.velocity.x = -pos[0] * _speed;
			particle.velocity.y = -pos[1] * _speed;
			particle.onEmit();
		}
		
		public function makeColoredParticles(width:uint = 2, height:uint = 2, color:uint = 0xFFFFFFFF):void
		{
			var randSize:uint;
			var whitePixel:FlxParticle;
			for (var i:int = 0; i < _maxSize/2; i++) {
				whitePixel = new FlxParticle();
				whitePixel.makeGraphic(width, height, color);
				whitePixel.visible = false;
				add(whitePixel);
				whitePixel = new FlxParticle();
				if (width == height) {
					randSize = (width - 1) * FlxG.random() + 1;
					whitePixel.makeGraphic(randSize, randSize, color);
				} else {
					whitePixel.makeGraphic((width - 1) * FlxG.random() + 1, (height - 1) * FlxG.random() + 1, color);
				}
				whitePixel.visible = false;
				add(whitePixel);
			}
		}
		
		private function randRadius():Number
		{
			return (this.radius * FlxG.random()) + 12.5;
		}
		
		private function randAngle():Number
		{
			return (2 * Math.PI * FlxG.random());
		}
		
		private function randPos():Array
		{
			var tempRad:Number = randRadius();
			var tempAngle:Number = randAngle();
			
			return [ tempRad * Math.cos(tempAngle), tempRad * Math.sin(tempAngle) ];
		}
		
		public function get speed():Number
		{
			return _speed;
		}
		
		public function set speed(newSpeed:Number):void
		{
			this._speed = newSpeed;
		}
		
	}

}